﻿using System.Windows ;
using System.IO.IsolatedStorage ;
using System ;

namespace OpenLightGroup.Common
{
    public class WebSource
    {
        private const string SETTING_SourceDirectoryPath = "SourceDirectoryPath" ;

        /// <summary>
        /// Fix for OOB applications looking at a file path...
        /// </summary>
        /// <returns></returns>
        public static Uri GetSourceDirectory ( )
        {
            Uri sourcePath ;

            if ( Application.Current.IsRunningOutOfBrowser )
            {
                if ( IsolatedStorageSettings.ApplicationSettings.Contains( SETTING_SourceDirectoryPath ) )
                    sourcePath = IsolatedStorageSettings.ApplicationSettings[SETTING_SourceDirectoryPath] as Uri ;
                else
                    sourcePath = new Uri( "http://localhost/ClientBin", UriKind.RelativeOrAbsolute ) ;
            }
            else
            {
                var src = Application.Current.Host.Source.ToString( ) ;

                sourcePath = new Uri( src.Substring( 0, src.LastIndexOf( '/' ) + 1 ) ) ;

                IsolatedStorageSettings.ApplicationSettings[SETTING_SourceDirectoryPath] = sourcePath ;

                IsolatedStorageSettings.ApplicationSettings.Save( ) ;

            }

            return sourcePath ;
        }
        
    }
}